package com.gxc.controller;

import com.gxc.dao.jpa.UserRepository;
import com.gxc.entity.User;
import com.gxc.utils.RestControllerResult;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * User Controller.
 *
 * @author 宫新程
 * @since 2019/2/13 19:57
 */
@RestController
public class UserController {

  @Resource private UserRepository userRepository;

  /** 登录. */
  @PostMapping("/login")
  public RestControllerResult<Boolean> login(User user, HttpSession session) {
    RestControllerResult<Boolean> result = new RestControllerResult<>();
    List<User> usernameList = userRepository.findUserByUsername(user.getUsername());
    if (!usernameList.isEmpty()) {
      User dbUser = usernameList.get(0);
      if (user.getPassword().equals(dbUser.getPassword())) {
        result.setSuccess(true);
        session.setAttribute("login_user", dbUser);
      } else {
        result.setErrorMsg("密码不正确！");
        result.setSuccess(false);
      }
    } else {
      result.setErrorMsg("用户名不存在！");
      result.setSuccess(false);
    }
    return result;
  }
}
